package cn.dnui_lxx432.dnuifood_16110100432.controller;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.Adapter.ShopAdapter;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.ShopBean;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.listener.OnRetrofitListener;
import cn.dnui_lxx432.dnuifood_16110100432.model.ShopModel;

public class HomeFragment extends android.support.v4.app.Fragment implements OnRetrofitListener<List<ShopBean>> {
  private ShopBean shop;
  private RecyclerView recyclerView;
  private ShopAdapter adapter;


  public HomeFragment()  {

  }


  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    //onViewCreated是在onCreateView后被触发的事件
    super.onViewCreated(view, savedInstanceState);

    recyclerView=view.findViewById(R.id.shop_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(new ShopAdapter(getActivity(),getData()));
    initViews();
    initData();

  }

  void initViews() {
    recyclerView = recyclerView.findViewById(R.id.shop_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  }


  void initData(){
    ShopModel shopModel = new ShopModel();
    shopModel.getAllShops(this);  //访问服务器请求
  }



  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {


    return inflater.inflate(R.layout.fragment_home, container, false);
  }



  private List<ShopBean> getData(){
    List<ShopBean> list = new ArrayList<>();
    ShopBean shop = new ShopBean();
    list.add(shop);
    return list;
  }



  @Override
  public void onSuccess(List<ShopBean> shops, int flag) {
    adapter = new ShopAdapter(getActivity(),shops);
    recyclerView.setAdapter(adapter);
  }

  @Override
  public void onFailure(String msg) {
    Toast.makeText(getActivity(),"网络错误",Toast.LENGTH_SHORT).show();
  }
}
